package com.shop.cereshop.app.tencent.im.model.redpacket;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.shop.cereshop.commons.tencent.im.constant.CustomMsgModelType;
import com.shop.cereshop.commons.tencent.im.model.msg.data.CustomMsgData;
import com.shop.cereshop.commons.tencent.im.model.msg.data.CustomMsgDataModel;
import com.shop.cereshop.commons.tencent.im.model.msg.dto.MsgRequestDTO;
import com.shop.cereshop.commons.tencent.im.utils.JsonUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 红包信息VO
 * @author zdd
 * @date 2025/9/3 14:30
 * @version 1.0
 */
@ApiModel("红包信息VO")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RedPacketVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("红包ID")
    private String packetId;

    @ApiModelProperty("单号")
    private String orderId;

    @ApiModelProperty("红包生命周期：1-有效 2-已领完 3-已过期")
    private Integer packetLifeCycle;

    @ApiModelProperty("交易类型：5-发红包付款 6-领取红包收款 7-红包退款")
    private  Integer transactionType;

    @ApiModelProperty("发送者ID")
    private String senderId;

    @ApiModelProperty("发送者名称")
    private String senderName;

    @ApiModelProperty("发送者头像")
    private String senderHeadImage;

    @ApiModelProperty("群组ID")
    private String groupId;

    @ApiModelProperty("接收者ID")
    private String receiverId;

    @ApiModelProperty("接收者头像")
    private String receiverHeadImage;

    @ApiModelProperty("已领取人ID列表")
    private List<String> receiverIds = new ArrayList<>();

    @ApiModelProperty("接收者名称")
    private String receiverName;

    @ApiModelProperty("红包类型：1-普通红包 2-拼手气红包 3-专属红包")
    private Integer packetType;

    @ApiModelProperty("祝福语")
    private String blessing;

    @ApiModelProperty("红包封面")
    private String cover;

    @ApiModelProperty("红包总金额 (单位：元)")
    private BigDecimal totalAmount;

    @ApiModelProperty("总个数")
    private Integer totalCount;

    @ApiModelProperty("剩余金额 (单位：元)")
    private BigDecimal remainAmount;

    @ApiModelProperty("剩余个数")
    private Integer remainCount;

    @ApiModelProperty("状态：0-创建未发送 1-未领取 2-未领完 3-已领完 4-已过期 5-已退款")
    private Integer status;

    @ApiModelProperty("创建时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    @ApiModelProperty("红包发送时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime sendTime;

    @ApiModelProperty("红包过期时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime expireTime;

    /**
     * 构建红包自定义消息数据（腾讯IM消息格式）
     * @return 自定义消息数据
     */
    public CustomMsgData covertToCustomMsgData() {
        CustomMsgData msgData = new CustomMsgData();
        msgData.setData(this.covertToMsgData());
        msgData.setDesc(this.getBlessing());
        msgData.setExt("");
        msgData.setSound("");
        return msgData;
    }

    /**
     * 构建红包自定义消息传输数据
     * @return 自定义消息传输数据
     */
    public MsgRequestDTO covertToMsgRequestDTO() {
        return MsgRequestDTO.builder()
                .fromAccount(this.getSenderId())
                .toAccount(this.getReceiverId())
                .groupId(this.getGroupId())
                .title("红包来了")
                .desc(this.getBlessing())
                .build();
    }

    /**
     * 构建红包自定义消息体（前端所需）
     * @return 自定义消息数据
     */
    public String covertToMsgData() {
        CustomMsgDataModel<Object> dataModel = new CustomMsgDataModel<>();
        dataModel.setVersion("1.0");
        dataModel.setType(CustomMsgModelType.RED_PACKET);
        dataModel.setData(this);
        dataModel.setTimestamp(System.currentTimeMillis());
        return JsonUtils.toJson(dataModel);
    }

}
